import {USER_TYPE} from '@/common/constants'
import storage from '@/common/storage'

const state = {
    //用户信息
    userInfo: storage.get('userInfo'),
    //用户登录令牌
    token: localStorage.getItem('token'),
    //登录失败次数
    loginFailNum: 0,
    //用户未读消息
    unreadMessage: {
        comment: 0,
        reply: 0,
        like: 0,
        sys: 0,
        mine: 0
    }
}

const getters = {
    userInfo: state => state.userInfo || {},
    token: state => state.token || '',
    unreadMessage: state => state.unreadMessage || {},
    loginFailNum: state => state.loginFailNum || 0,
    //登录是否验证
    isAuthenticated: (state, getters) => {
        return !!getters.token
    },
    //是否为管理员
    isAdminRole: (state, getters) => {
        if (getters.userInfo.roleList) {
            return getters.userInfo.roleList.indexOf(USER_TYPE.ADMIN) != -1 ||
                getters.userInfo.roleList.indexOf(USER_TYPE.PROBLEM_ADMIN) != -1 ||
                getters.userInfo.roleList.indexOf(USER_TYPE.SUPER_ADMIN) != -1
        } else {
            return false;
        }
    },
    //是否为超级管理员
    isSuperAdmin: (state, getters) => {
        if (getters.userInfo.roleList) {
            return getters.userInfo.roleList.indexOf(USER_TYPE.SUPER_ADMIN) != -1
        } else {
            return false;
        }
    },
    //是否为题目管理员
    isProblemAdmin: (state, getters) => {
        if (getters.userInfo.roleList) {
            return getters.userInfo.roleList.indexOf(USER_TYPE.PROBLEM_ADMIN) != -1
        } else {
            return false;
        }
    },
}

const mutations = {
    /**
     * 修改用户信息
     * @param state
     * @param userInfo
     */
    changeUserInfo(state, {userInfo}) {
        state.userInfo = userInfo
        storage.set('userInfo', userInfo)
    },
    /**
     * 修改用户登录验证令牌
     * @param state
     * @param token
     */
    changeUserToken(state, token) {
        state.token = token
        localStorage.setItem("token", token)
    },
    /**
     * 记录用户登录失败次数
     * @param state
     * @param success
     */
    incrLoginFailNum(state, {success}) {
        if (!success) {
            state.loginFailNum += 1
        } else {
            state.loginFailNum = 0
        }
    },
    /**
     * 清空用户信息和登录token
     * @param state
     */
    clearUserInfoAndToken(state) {
        state.token = ''
        state.userInfo = {}
        state.loginFailNum = 0
        storage.clear()
        console.log("clearUserInfoAndToken-state.token:" + state.token + ", userInfo:" + state.userInfo === {})
    },
    /**
     * 更新用户未读消息
     * @param state
     * @param unreadMessage
     */
    updateUnreadMessageCount(state, {unreadMessage}) {
        state.unreadMessage = unreadMessage
    },
    /**
     * 计算用户已读消息
     * @param state
     * @param needSubtractMsg
     */
    subtractUnreadMessageCount(state, {needSubtractMsg}) {
        //负数也没关系
        state.unreadMessage[needSubtractMsg.name] = state.unreadMessage[needSubtractMsg.name] - needSubtractMsg.num;
    }
}

const actions = {
    setUserInfo({commit}, userInfo) {
        commit('changeUserInfo', {
            userInfo: userInfo
        })
    },
    incrLoginFailNum({commit}, success) {
        commit('incrLoginFailNum', {success: success})
    },
    clearUserInfoAndToken({commit}) {
        commit('clearUserInfoAndToken')
    },
    updateUnreadMessageCount({commit}, unreadMessage) {
        commit('updateUnreadMessageCount', {
            unreadMessage: unreadMessage
        })
    },
    subtractUnreadMessageCount({commit}, needSubtractMsg) {
        commit('subtractUnreadMessageCount', {
            needSubtractMsg: needSubtractMsg
        })
    }
}

export default {
    state,
    getters,
    actions,
    mutations
}
